/**
 * @file ui_app_main.c
 * @author your name (you@domain.com)
 * @brief 
 * @version 0.1
 * @date 2023-06-13
 * 
 * @copyright Copyright (c) 2023
 * 
 */

#include "ui_app_main.h"
#include "msg_event_config.h"
#include "ui_img_btn.h"
LV_IMG_DECLARE(app_icon);

static ui_basic_com_t s_ui_app_main = {0};
static bool s_already_init = false;
static lv_obj_t *bg_obj = NULL;
static lv_obj_t *img_btn_obj = NULL;

static void img_event_cb(lv_event_t *event) 
{
     ui_change_ui(EASY_UI_APP_IMG, true);
}

static int ui_app_main_enter(int arg0, int arg1, void *arg3)
{
    if(s_already_init)
    {
       return -1;
    }
    s_already_init = true;

    lv_disp_t *d_disp = lv_disp_get_default();
    lv_color_t bg_color = {0};
    bg_color = lv_color_hex(HOME_UI_BG_COLOR);
    bg_obj = lv_obj_create(lv_scr_act());
    lv_obj_set_size(bg_obj, d_disp->driver->hor_res, d_disp->driver->ver_res);
    lv_obj_set_pos(bg_obj, 0,  0);
    lv_obj_set_style_bg_color(bg_obj, bg_color, 0);
    //lv_obj_set_style_bg_opa(bg_obj, LV_OPA_100, 0);
    lv_obj_set_style_radius(bg_obj, 0, 0);
    lv_obj_set_style_border_width(bg_obj, 0, 0);
    //lv_obj_add_flag(bg_obj, LV_OBJ_FLAG_SCROLL_CHAIN_VER);
    lv_obj_clear_flag(bg_obj, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_clear_flag(lv_scr_act(), LV_OBJ_FLAG_SCROLLABLE);
     
    img_btn_obj = ui_img_btn_create(bg_obj, &app_icon, HOME_UI_ITEM_APP, img_event_cb);
   

    return 0;
}

static int ui_app_main_loop(int arg0, int arg1, void *arg3)
{
    if(!s_already_init)
    {
       return -1;
    }

    return 0;
}

static int ui_app_main_exit(int arg0, int arg1, void *arg3)
{
    if(!s_already_init)
    {
       return -1;
    }
    
    s_already_init = false;

    lv_obj_del(bg_obj);

    return 0;
}

static void ui_app_main_event(char *event_name, void *data, int srcId)
{
 
}

ui_basic_com_t *ui_app_main_create(void)
{
    s_ui_app_main.ui_id = EASY_UI_APP_MAIN;
    memcpy(s_ui_app_main.ui_name, EASY_UI_NAME(EASY_UI_APP_MAIN), strlen(EASY_UI_NAME(EASY_UI_APP_MAIN)));
    s_ui_app_main.enter = ui_app_main_enter;
    s_ui_app_main.run = ui_app_main_loop;
    s_ui_app_main.exit = ui_app_main_exit;
    return &s_ui_app_main;
}